package com.wjb.xietong.server.task;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.wjb.xietong.server.BaseResponseDataParse;
import com.wjb.xietong.server.HeaderInfoResponse;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;

public class GetTaskResponse extends BaseResponseDataParse {

	private static GetTaskResponse instance = null;

	private HeaderInfoResponse headerInfo = null;
	private List<GetTaskInfo> getTaskInfoList = null;

	private GetTaskResponse() {

	}

	public static synchronized GetTaskResponse instance() {
		if (null == instance) {
			instance = new GetTaskResponse();
		}
		return instance;
	}

	@Override
	public void initData() {
		setHeaderInfo(null);
		setGetTaskInfoList(null);
	}

	@Override
	public boolean parseJsonObj(JSONObject response) {
		initData();

		if (null == response) {
			LogD.output("Exception: response is null");
			return false;
		}

		if (!checkRespCode(response)) {
			headerInfo = new HeaderInfoResponse();
			headerInfo.parseJsonObj(response);
			setHeaderInfo(headerInfo);
			return false;
		}

		if (response.has(IDs.WORK_SYS_TASK_DO_MAP_LIST)) {
			JSONArray array = response
					.optJSONArray(IDs.WORK_SYS_TASK_DO_MAP_LIST);
			if (null == array) {
				LogD.output("Warning: No Data!");
				return true;
			}

			getTaskInfoList = new ArrayList<GetTaskResponse.GetTaskInfo>();
			for (int i = 0; i < array.length(); i++) {
				JSONObject jsonObj = array.optJSONObject(i);
				if (null == jsonObj) {
					continue;
				}

				GetTaskInfo taskInfo = new GetTaskInfo();
				if (jsonObj.has(IDs.CURRENT_OPERATOR_USER_ID)) {
					taskInfo.setCurrentOperatorUserId(jsonObj
							.optString(IDs.CURRENT_OPERATOR_USER_ID));
				}
				if (jsonObj.has(IDs.TASK_ID)) {
					taskInfo.setTaskId(jsonObj.optLong(IDs.TASK_ID));
				}
				if (jsonObj.has(IDs.TITLE)) {
					taskInfo.setTitle(jsonObj.optString(IDs.TITLE));
				}
				if (jsonObj.has(IDs.DUEDATE)) {
					taskInfo.setDueDate(jsonObj.optString(IDs.DUEDATE));
				}
				if (jsonObj.has(IDs.CURRENT_OPERATOR_USER_NAME)) {
					taskInfo.setCurrentOperatorUserName(jsonObj
							.optString(IDs.CURRENT_OPERATOR_USER_NAME));
				}
				getTaskInfoList.add(taskInfo);
			}
			setGetTaskInfoList(getTaskInfoList);
		}
		return true;
	}

	public static class GetTaskInfo {
		private String currentOperatorUserId = null;
		private long taskId = 0l;
		private String title = null;
		private String dueDate = null;// 09-16 01:54
		private String currentOperatorUserName = null;

		public String getCurrentOperatorUserId() {
			return currentOperatorUserId;
		}

		public void setCurrentOperatorUserId(String currentOperatorUserId) {
			this.currentOperatorUserId = currentOperatorUserId;
		}

		public long getTaskId() {
			return taskId;
		}

		public void setTaskId(long taskId) {
			this.taskId = taskId;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getDueDate() {
			return dueDate;
		}

		public void setDueDate(String dueDate) {
			this.dueDate = dueDate;
		}

		public String getCurrentOperatorUserName() {
			return currentOperatorUserName;
		}

		public void setCurrentOperatorUserName(String currentOperatorUserName) {
			this.currentOperatorUserName = currentOperatorUserName;
		}

	}

	public HeaderInfoResponse getHeaderInfo() {
		return headerInfo;
	}

	public void setHeaderInfo(HeaderInfoResponse headerInfo) {
		this.headerInfo = headerInfo;
	}

	public List<GetTaskInfo> getGetTaskInfoList() {
		return getTaskInfoList;
	}

	public void setGetTaskInfoList(List<GetTaskInfo> getTaskInfoList) {
		this.getTaskInfoList = getTaskInfoList;
	}

}
